package com.sun.web.framework;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.sun.web.domain.User;
import com.sun.web.service.UserService;


public class TokenHolderInterceptor implements HandlerInterceptor{
	
	@Autowired
	private UserService userService;
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
			Object arg2) throws Exception {
		
		System.out.println("preHandler.thread="+Thread.currentThread());
		
		String token = request.getParameter("token");
		System.out.println("preHandle token = " + token);
		User user = userService.getUserByToken(token);
		TokenHolder.setUser(user);
		
		return true;
	}

	@Override
	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		System.out.println("afterCompletion.thread="+Thread.currentThread());
		
		TokenHolder.clear();
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2, ModelAndView arg3) throws Exception {
		System.out.println("postHandle.thread="+Thread.currentThread());
		
	}


}
